home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / num2word.zip / NUM2WORD.C
Text File  |  1991-08-25  |  4KB  |  131 lines

  1.  
  2. #ifdef COMMENT_OUT
  3.  
  4. Organization: Columbia University Department of Computer Science
  5. Message-ID: <TIM.91Aug15142410@tompkins.cs.columbia.edu>
  6. Newsgroups: alt.sources
  7.  
  8. Here's a short program which takes a phone number (of any length) as a command
  9. line argument and prints words which match that number according to
  10. digit/letter relations on the keypad.  For example, "number2word 2337" would
  11. return "beep" and "beer".
  12.  
  13. Note that the digits 0 and 1 on the keypad don't have any letters, so no
  14. words will match phone numbers containing them.  The program could be made
  15. smarter by dealing with this in some way, splitting numbers into multiple
  16. words, checking for digits like 2 and 4 which sound the same as words, or
  17. applying other heuristics, but hey, I only have so much free time :).
  18.  
  19. Enjoy.
  20.  
  21. #endif
  22.  
  23. /*
  24.  * number2word.c -- a quick hack to find words that match a phone number
  25.  *
  26.  * Timothy Jones (tim@cs.columbia.edu), June 1991
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. #define WordList "/usr/dict/words"
  33.  
  34. main (argc, argv)
  35.     int argc;
  36.     char **argv;
  37. {
  38.     char number[100], line[81];
  39.     FILE *fp;
  40.     int i, length;
  41.  
  42.     /* check the arguments */
  43.     if (argc != 2)
  44.     {
  45.         (void) fprintf (stderr, "Usage: %s <phone number>\n", argv[0]);
  46.         exit (1);
  47.     }
  48.  
  49.     /* remember some important things */
  50.     (void) strcpy (number, argv[1]);
  51.     length = strlen (number);
  52.  
  53.     /* open the words file */
  54.     if ((fp = fopen (WordList, "r")) == NULL)
  55.     {
  56.         (void) fprintf (stderr, "Couldn't open %s!\n", WordList);
  57.         exit (1);
  58.     }
  59.  
  60.     /* read in the words, one at a time, and check for matches */
  61.     while (fgets (line, sizeof (line), fp))
  62.     {
  63.         /* only look at words with the correct length */
  64.         if ((i = strlen (line)) == length+1)
  65.         {
  66.             /* get rid of the trailing newline */
  67.             line[i-1] = 0;
  68.  
  69.             /* check each character of the word */
  70.             for (i = 0; i < length; i++)
  71.             {
  72.                 switch (number[i] - '0')
  73.                 {
  74.                     /*
  75.                      * since the numbers '0' and '1' don't have any
  76.                      * associated letters, we ignore them
  77.                      */
  78.                 case 2:
  79.                     if (line[i] != 'a' && line[i] != 'b' && line[i] != 'c')
  80.                         goto next;
  81.                     break;
  82.                 case 3:
  83.                     if (line[i] != 'd' && line[i] != 'e' && line[i] != 'f')
  84.                         goto next;
  85.                         break;
  86.                 case 4:
  87.                     if (line[i] != 'g' && line[i] != 'h' && line[i] != 'i')
  88.                         goto next;
  89.                     break;
  90.                 case 5:
  91.                     if (line[i] != 'j' && line[i] != 'k' && line[i] != 'l')
  92.                         goto next;
  93.                     break;
  94.                 case 6:
  95.                     if (line[i] != 'm' && line[i] != 'n' && line[i] != 'o')
  96.                         goto next;
  97.                     break;
  98.                 case 7:
  99.                     if (line[i] != 'p' && line[i] != 'r' && line[i] != 's')
  100.                         goto next;
  101.                     break;
  102.                 case 8:
  103.                     if (line[i] != 't' && line[i] != 'u' && line[i] != 'v')
  104.                         goto next;
  105.                     break;
  106.                 case 9:
  107.                     if (line[i] != 'w' && line[i] != 'x' && line[i] != 'y')
  108.                         goto next;
  109.                     break;
  110.                 }
  111.             }
  112.  
  113.             if (i == length)
  114.             {
  115.                 (void) printf ("%s\n", line);
  116.             }
  117.         }
  118.     next:
  119.         ;
  120.     }
  121.  
  122.     exit (0);
  123. }
  124.  
  125. #ifdef COMMENT_OUT
  126. --
  127.                                         - Tim Jones
  128.                                           tim@cs.columbia.edu
  129.  
  130. #endif
  131.